﻿#include "dialAddStuInfo.h"
#include "ui_dialAddStuInfo.h"
#include <QRegExpValidator>
#include <QMessageBox>

DialAddStuInfo::DialAddStuInfo(QWidget *parent) :
    QDialog(parent),
    dialAdd_ui(new Ui::DialAddStuInfo)
{
    dialAdd_ui->setupUi(this);

    // 性别
    dialAdd_ui->comboBox_gender->addItem("男");
    dialAdd_ui->comboBox_gender->addItem("女");
    // 政治身份
    dialAdd_ui->comboBox_politID->addItem("群众");
    dialAdd_ui->comboBox_politID->addItem("中共党员");
    dialAdd_ui->comboBox_politID->addItem("中共预备党员");
    dialAdd_ui->comboBox_politID->addItem("共青团员");
    dialAdd_ui->comboBox_politID->addItem("其他");
    // 绩点 0.00 - 4.00
    dialAdd_ui->doubBox_gpa->setRange(0.00,4.00);

    // 正则表达式
    // 学号 6位数
    QRegExp regId("^[0-9]{6}$");
    dialAdd_ui->lineEdit_id->setValidator(new QRegExpValidator(regId,this));
    // 手机号 11位数字
    QRegExp regPhoNum("^[1-9][0-9]{10}$");
    dialAdd_ui->lineEdit_phoneNum->setValidator(new QRegExpValidator(regPhoNum,this));
}

DialAddStuInfo::~DialAddStuInfo()
{
    delete dialAdd_ui;
}

void DialAddStuInfo::showItemInfo(int p_id, QString p_name, QString p_gender, QString p_politId, QString p_phoneNum, QString p_address, float p_gpa)
{
    dialAdd_ui->lineEdit_id->setText(QString("%1").arg(p_id,6,10,QLatin1Char('0')));
    dialAdd_ui->lineEdit_name->setText(p_name);
    dialAdd_ui->comboBox_gender->setCurrentText(p_gender);
    dialAdd_ui->lineEdit_phoneNum->setText(p_phoneNum);
    dialAdd_ui->comboBox_politID->setCurrentText(p_politId);
    dialAdd_ui->textEdit_address->setText(p_address);
    dialAdd_ui->doubBox_gpa->setValue(p_gpa);
}

void DialAddStuInfo::on_btnAddStu_clicked()
{
    // 确认输入信息合法
    int idLen = dialAdd_ui->lineEdit_id->text().size();
    if(idLen!=6){
        QMessageBox::information(this,tr("提示"),tr("学号少于6位数字"));
        return;
    }
    QString name = dialAdd_ui->lineEdit_name->text();
    if(name.isEmpty()){
        QMessageBox::information(this,tr("提示"),tr("姓名不能为空"));
        return;
    }
    int phnLen = dialAdd_ui->lineEdit_phoneNum->text().size();
    if(phnLen!=11){
        QMessageBox::information(this,tr("提示"),tr("手机号少于11位数字"));
        return;
    }
    QString address = dialAdd_ui->textEdit_address->toPlainText();
    if(address.isEmpty()){
        QMessageBox::information(this,tr("提示"),tr("住址信息不能为空"));
        return;
    }
    // 读取填入的信息
    int id = dialAdd_ui->lineEdit_id->text().toUInt();
    QString phoNum = dialAdd_ui->lineEdit_phoneNum->text();
    QString politId = dialAdd_ui->comboBox_politID->currentText();
    QString gender = dialAdd_ui->comboBox_gender->currentText();
    float gpa = dialAdd_ui->doubBox_gpa->value();

    CStuInfo stuInfo;
    stuInfo.setStuInfo(id,name,gender,politId,phoNum,address,gpa);

    // 如果添加学生信息
    if(!getIsModOrAdd()){
        // 使用信号和槽的方式，把要添加的学生信息，发送到 我的主窗口
        emit sig_addInfo(stuInfo);
        // 清空数据
        on_btnClearAll_clicked();
        return;
    }
    // 如果修改学生信息
    // 删除前进行提示，是否决定删除数据
    int ret = QMessageBox::question(this, tr("提示"),
                                   tr("确定修改学生信息吗？"),
                                   QMessageBox::Yes | QMessageBox::No);

    // 如果用户选择取消，直接返回
    if(ret == QMessageBox::No) return;

    emit sig_modInfo(stuInfo);
}

void DialAddStuInfo::on_btnClearAll_clicked()
{
    dialAdd_ui->lineEdit_id->clear();
    dialAdd_ui->lineEdit_name->clear();
    dialAdd_ui->lineEdit_phoneNum->clear();
    dialAdd_ui->textEdit_address->clear();
    dialAdd_ui->comboBox_gender->setCurrentIndex(0);
    dialAdd_ui->comboBox_politID->setCurrentIndex(0);
    dialAdd_ui->doubBox_gpa->setValue(0.00);
}

void DialAddStuInfo::on_btnCancel_clicked()
{
    this->close();
}

bool DialAddStuInfo::getIsModOrAdd() const
{
    return isModOrAdd;
}

void DialAddStuInfo::setIsModOrAdd(bool value)
{
    isModOrAdd = value;
}

void DialAddStuInfo::setAddBtnText(QString text)
{
    dialAdd_ui->btnAddStu->setText(text);
}
